<?php
/*
Extension Name: FCKeditor
Extension Url: http://www.fckeditor.net/download/
Description: Add a WYSIWYG editor to the comment form
Version: 2.6
Author: Ziyad (MySchizoBuddy) Saeed, Dinoboff,
Author Url: http://myschizobuddy.com

*/

if (!defined('FCKEDITOR_PATH')) define('FCKEDITOR_PATH', dirname(__FILE__) . '/');
require_once(FCKEDITOR_PATH . 'settings.php');
if (!function_exists('kses')) require_once(FCKEDITOR_PATH . 'kses.php');


$Context->SetDefinition('FCKeditorPreferences', 'FCKeditor Preferences');
$Context->SetDefinition('FCKeditorConfiguration', 'FCKeditor Configuration');
$Context->SetDefinition('FCKeditorAttachment', 'Attachments');
$Context->SetDefinition('FCKeditorAttachmentEnable', 'Enable Attachments');
$Context->SetDefinition('FCKeditorAttachmentNotes','The path to the upload directory should be an absolute path. Use a path outside your webroot for better security. Remember that the folder should be writable. You can use the following replacement tags: %userid%');
$Context->SetDefinition('FCKeditorDefaultToolbar', 'Default Toolbar');
$Context->SetDefinition('FCKeditorEnableImage', 'Allow uploading/browsing of images');
$Context->SetDefinition('FCKeditorEnableFlash', 'Allow uploading/browsing of flash files');
$Context->SetDefinition('FCKeditorEnableLink', 'Allow uploading/browsing of other files');
$Context->SetDefinition('FCKeditorEnableLinkWarning', 'Be very careful which files you allow');
$Context->SetDefinition('FCKeditorDefaultLanguage', 'Default language');
$Context->SetDefinition('FCKeditorDefaultLanguageDirection', 'Default language direction');
$Context->SetDefinition('FCKeditorDefaultLinkTarget', 'Default link target');
$Context->SetDefinition('FCKeditorDefaultSkin', 'Default skin');
$Context->SetDefinition('FCKeditorShowBlocks', 'Show blocks');
$Context->SetDefinition('FCKeditorEnterMode', 'Enter mode');
$Context->SetDefinition('FCKeditorShiftEnterMode', 'Shift Enter mode');
$Context->SetDefinition('FCKeditorPlugins', 'Plugins');
$Context->SetDefinition('FCKeditorPluginsVideo', 'Embed youtube/google etc videos');
$Context->SetDefinition('VisualEditing', 'Visual Editing');
$Context->SetDefinition('VisualEditingPreference', 'Disable visual editing toolbar');

if( !array_key_exists('FCKEDITOR_UPLOAD_PATH', $Configuration)) {AddConfigurationSetting($Context, 'FCKEDITOR_UPLOAD_PATH', $Configuration['APPLICATION_PATH'] . 'uploads/%userid%/');}
if( !array_key_exists('FCKEDITOR_DEFAULT_LANGUAGE', $Configuration)) {AddConfigurationSetting($Context, 'FCKEDITOR_DEFAULT_LANGUAGE', 'en');}
if( !array_key_exists('FCKEDITOR_DEFAULT_TOOLBAR', $Configuration)) {AddConfigurationSetting($Context, 'FCKEDITOR_DEFAULT_TOOLBAR', 'Vanilla');}
if( !array_key_exists('FCKEDITOR_LANGUAGE_DIRECTION', $Configuration)) {AddConfigurationSetting($Context, 'FCKEDITOR_LANGUAGE_DIRECTION', 'ltr');}
if( !array_key_exists('FCKEDITOR_DEFAULT_LINK_TARGET', $Configuration)) {AddConfigurationSetting($Context, 'FCKEDITOR_DEFAULT_LINK_TARGET', '_blank');}
if( !array_key_exists('FCKEDITOR_DEFAULT_SKIN', $Configuration)) {AddConfigurationSetting($Context, 'FCKEDITOR_DEFAULT_SKIN', 'silver');}
if( !array_key_exists('FCKEDITOR_SHOW_BLOCKS', $Configuration)) {AddConfigurationSetting($Context, 'FCKEDITOR_SHOW_BLOCKS', 'true');}
if( !array_key_exists('FCKEDITOR_ENTERMODE', $Configuration)) {AddConfigurationSetting($Context, 'FCKEDITOR_ENTERMODE', 'br');}
if( !array_key_exists('FCKEDITOR_SHIFT_ENTERMODE', $Configuration)) {AddConfigurationSetting($Context, 'FCKEDITOR_SHIFT_ENTERMODE', 'p');}
if( !array_key_exists('FCKEDITOR_FILEMANAGER', $Configuration)) {AddConfigurationSetting($Context, 'FCKEDITOR_FILEMANAGER', 'false');}
if( !array_key_exists('FCKEDITOR_IMAGE', $Configuration)) {AddConfigurationSetting($Context, 'FCKEDITOR_IMAGE', 'false');}
if( !array_key_exists('FCKEDITOR_IMAGE_ALLOWED_EXTENSIONS', $Configuration)) {AddConfigurationSetting($Context, 'FCKEDITOR_IMAGE_ALLOWED_EXTENSIONS', 'jpg|gif|jpeg|png|bmp');}
if( !array_key_exists('FCKEDITOR_FLASH', $Configuration)) {AddConfigurationSetting($Context, 'FCKEDITOR_FLASH', 'false');}
if( !array_key_exists('FCKEDITOR_FLASH_ALLOWED_EXTENSIONS', $Configuration)) {AddConfigurationSetting($Context, 'FCKEDITOR_FLASH_ALLOWED_EXTENSIONS', 'swf|flv');}
if( !array_key_exists('FCKEDITOR_LINK', $Configuration)) {AddConfigurationSetting($Context, 'FCKEDITOR_LINK', 'false');}
if( !array_key_exists('FCKEDITOR_LINK_ALLOWED_EXTENSIONS', $Configuration)) {AddConfigurationSetting($Context, 'FCKEDITOR_LINK_ALLOWED_EXTENSIONS', 'doc|pdf|zip');}
if( !array_key_exists('FCKEDITOR_PLUGIN_VIDEO', $Configuration)) {AddConfigurationSetting($Context, 'FCKEDITOR_PLUGIN_VIDEO', 'true');}






/**
 * Settings Panel. This is where all the forms are generated.
 */
if(($Context->SelfUrl == 'settings.php') && $Context->Session->User->Permission('PERMISSION_CHANGE_APPLICATION_SETTINGS')) {
	require_once(FCKEDITOR_PATH . 'Class.FCKeditorForm.php');

	$FCKeditorForm = $Context->ObjectFactory->NewContextObject($Context, 'FCKeditorForm');
	$Page->AddRenderControl($FCKeditorForm, $Configuration["CONTROL_POSITION_BODY_ITEM"]);
	$ExtensionOptions = $Context->GetDefinition('Extension Options');
	$Panel->AddList($ExtensionOptions);
	$Panel->AddListItem($ExtensionOptions, $Context->GetDefinition('FCKeditorPreferences'), GetUrl($Context->Configuration, 'settings.php', '', '', '', '', 'PostBackAction=FCKeditorPreference'));
}

//add Kses formater
class FCKeditorFormatter extends StringFormatter
{
	var $allowed_tags;
	var $allowed_protocols;

	function FCKeditorFormatter($tags, $protocols)
	{
		$this->allowed_tags = $tags;
		$this->allowed_protocols = $protocols;
	}

	function Parse($String, $Object, $FormatPurpose) {
		if ($FormatPurpose == FORMAT_STRING_FOR_DISPLAY)
		{
			return kses($String, $this->allowed_tags, $this->allowed_protocols);
		}
		return $String;
	}
}
$FCKeditorFormatter = $Context->ObjectFactory->NewObject($Context, "FCKeditorFormatter", $FCKeditor_allowed_tags, $FCKeditor_allowed_protocols);
$Context->StringManipulator->AddManipulator("FCKeditor", $FCKeditorFormatter);



if ($Context->Session->UserID > 0 && $Context->Session->User->Permission('PERMISSION_HTML_ALLOWED')) {
	if ($Context->Session->User->Preference('DisableVisualEditing') == 0) {
		//Make FCKeditor formatter the only formatter available to post a new comment or to edit an old one
		$Context->Configuration['DEFAULT_FORMAT_TYPE'] = 'FCKeditor';
		$Context->Session->User->DefaultFormatType = 'FCKeditor';
		$Context->Session->User->Preferences['ShowFormatSelector'] = 0;
	}
	else {
		$Context->Session->User->Preferences['ShowFormatSelector'] = 1;
	}
}


if ( in_array($Context->SelfUrl, array("post.php", "comments.php"))){
	if ($Context->Session->User->Preference('DisableVisualEditing') == 0 && $Context->Session->User->Permission('PERMISSION_HTML_ALLOWED')) {
		$Head->AddScript('extensions/Fckeditor/fckeditor/fckeditor.js');
		function RenderFckeditorToolbar(&$DiscussionForm) {
			echo '
			<script type="text/javascript">
				if (document.getElementById("CommentBox")) {
					document.getElementById("CommentBoxController").style.display = "none";
					var oFCKeditor = new FCKeditor("Body") ;
					oFCKeditor.BasePath = "' . $DiscussionForm->Context->Configuration['WEB_ROOT'] . 'extensions/Fckeditor/fckeditor/" ;
					oFCKeditor.Config["CustomConfigurationsPath"] = "' . $DiscussionForm->Context->Configuration['WEB_ROOT'] . 'extensions/Fckeditor/config.js.php" ;
					oFCKeditor.ToolbarSet = "'.$DiscussionForm->Context->Configuration['FCKEDITOR_DEFAULT_TOOLBAR'].'" ;
					oFCKeditor.Height = 400;
					oFCKeditor.CheckBrowser = true ;
					oFCKeditor.ReplaceTextarea() ;
				}
			</script>';
		}
		$Context->AddToDelegate('DiscussionForm', 'DiscussionForm_PreButtonsRender', 'RenderFckeditorToolbar');
		$Context->AddToDelegate('DiscussionForm', 'CommentForm_PreButtonsRender','RenderFckeditorToolbar');
	} else {
		function RemoveToolbarRadioButton($Context) {
			echo '
			<script type="text/javascript">
			if (document.getElementById("Radio_FCKeditor")){
				document.getElementById("Radio_FCKeditor").style.display = "none";
			}
			</script>';
		}
	
		$Context->AddToDelegate('DiscussionForm', 'DiscussionForm_PostSubmitRender', 'RemoveToolbarRadioButton');
		$Context->AddToDelegate('DiscussionForm', 'CommentForm_PostSubmitRender','RemoveToolbarRadioButton');
	}
}



if ($Context->SelfUrl == 'account.php' && $Context->Session->UserID > 0)
{
	function FCKeditor_PreferencesForm(&$PreferencesForm) {
		$PreferencesForm->AddPreference($PreferencesForm->Context->GetDefinition('VisualEditing'), $PreferencesForm->Context->GetDefinition('VisualEditingPreference'), 'DisableVisualEditing');
	}
	$Context->AddToDelegate('PreferencesForm', 'PreRender', 'FCKeditor_PreferencesForm');
}
?>
